/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eschool.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.eschool.model.Branch;
import com.eschool.model.User;
import com.eschool.util.HibernateUtil;
import com.eschool.util.JdbcUtil;

/**
 * 
 * @author Ammar
 */

public class UserDao {

	public static int addUser(User user) {

		int userId = -1;

		Connection connection = JdbcUtil.openConnection();

		try {
			CallableStatement storedProc = connection
					.prepareCall("{ call InsertUser(?,?,?,?,?,?,?,?) }");

			storedProc.setString(1, user.getName());
			storedProc.setString(2, user.getPhoneNumber());
			storedProc.setString(3, user.getAddress());
			storedProc.setString(4, user.getEmail());
			storedProc.setString(5, user.getPassword());
			storedProc.setInt(6, user.getBranch().getBranchId());
			storedProc.setInt(7, user.getRole().getRoleId());
			storedProc.registerOutParameter(8, java.sql.Types.INTEGER);

			storedProc.execute();

			userId = storedProc.getInt(8);

			connection.close();
		}

		catch (Exception e) {
			userId = -1;
			connection.close();
			e.printStackTrace();
		} finally {
			return userId;
		}
	}

	public static User getUserByProperty(String property, Object value) {

		Session session = HibernateUtil.currentSession();

		Criteria crit = session.createCriteria(User.class);
		crit.add(Restrictions.eq(property, value));

		List<User> user = crit.list();

		System.out.println(user.size());

		HibernateUtil.closeSession(session);

		if (user == null || user.size() == 0)
			return null;

		return user.get(0);
	}

	public static boolean updateUser(User user) {

		boolean isUpdated = false;

		Connection connection = JdbcUtil.openConnection();

		try {
			CallableStatement storedProc = connection
					.prepareCall("{ call UpdateUser(?,?,?,?,?,?,?,?) }");

			storedProc.setInt(1, user.getUserId());
			storedProc.setString(2, user.getName());
			storedProc.setString(3, user.getPhoneNumber());
			storedProc.setString(4, user.getAddress());
			storedProc.setString(5, user.getEmail());
			storedProc.setString(6, user.getPassword());
			storedProc.setInt(7, user.getBranch().getBranchId());
			storedProc.setInt(8, user.getRole().getRoleId());

			storedProc.execute();

			isUpdated = true;
			connection.close();
		}

		catch (Exception e) {
			isUpdated = false;
			connection.close();
			e.printStackTrace();
		} finally {
			return isUpdated;
		}
	}

	public static List getUsers(Map<String, Object> conditions) {

		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(User.class);

		if (conditions != null)
			for (Iterator iterator = conditions.entrySet().iterator(); iterator
					.hasNext();) {
				Map.Entry condition = (Map.Entry) iterator.next();
				crit.add(Restrictions.eq(((String) condition.getKey()),
						condition.getValue()));
			}

		List users = (List<User>) crit.list();

		HibernateUtil.closeSession(session);

		if (users == null)
			return Collections.EMPTY_LIST;

		return users;
	}
	
	public static List getUsersUsingOr(Map<String, Object> conditions) {

		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(User.class);

		crit.add(Restrictions.eq("branch", conditions.get("branch")));
		
		crit.add(Restrictions.or(
				Restrictions.eq("role", conditions.get("role"))
				, Restrictions.eq("role", conditions.get("role2"))));

		List users = (List<User>) crit.list();

		HibernateUtil.closeSession(session);

		if (users == null)
			return Collections.EMPTY_LIST;

		return users;
	}
}
